<?php
    class Facebook {

        private $appId;
        private $appKey;
        private $appSecret;
        private $baseUrl = 'https://graph.facebook.com/';
        private $token;
        private $usuarioId;

        public function Facebook() {
            $this->appId = '192094940827607';
            $this->appKey = 'ff09ff7d775af79aacba5ff25131c60b';
            $this->appSecret = 'b9d61293e3c4925865bc0044738f7eaf';
        }

        public function getAppId() {
            return $this->appId;
        }

        public function getAppKey() {
            return $this->appKey;
        }

        public function getAppSecret() {
            return $this->appSecret;
        }

        public function getUsuario() {
            return $this->jsonToArray(file_get_contents($this->baseUrl . $this->usuarioId));
        }

        public function setToken( $token ) {
            $this->token = $token;
        }

        public function getToken() {
            return $this->token;
        }
		
        public function setUsuarioId( $id ) {
            $this->usuarioId = $id;
        }

		public function getAmigos() {
			return $this->jsonToArray(file_get_contents($this->baseUrl . $this->usuarioId . '/friends?access_token=' . $this->token));
		}

        public function getFacebookCookie() {
            $args = array();
            parse_str(trim($_COOKIE['fbs_' . $this->appId], '\\"'), $args);
            ksort($args);
            $payload = '';
            foreach ($args as $key => $value) {
                if ($key != 'sig') {
                    $payload .= $key . '=' . $value;
                }
            }
            if (md5($payload . $this->appSecret) != $args['sig']) {
                return null;
            }
            return $args;
        }

        private function jsonToArray( $objJson ) {
			return json_decode($objJson);
        }

        private function arrayToJson( $array ) {
			return json_encode($array);
        }

    }

?>
